﻿#include"SList.h"
#include"contact.h"
void menu()
{
	printf("\n");
	printf("***************通讯录***************\n");
	printf("******1.添加联系人  2.删除联系人****\n");
	printf("******3.修改联系人  4.查找联系人****\n");
	printf("******5.查看通讯录  0.退出通讯录****\n");
	printf("************************************\n");
	printf("请选择操作：");
}

////初始化通讯录
//void InitContact(contact** con)
//{
//
//}


contact* FindByName(contact* con, char name[]) {
	contact* cur = con;
	while (cur)
	{
		if (strcmp(cur->val.name, name) == 0) {
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

//添加通讯录数据
void AddContact(contact** con)
{
	PeoInfo info;
	printf("请输⼊姓名：\n");
	scanf("%s", &info.name);
	printf("请输⼊性别：\n");
	scanf("%s", &info.sex);
	printf("请输⼊年龄：\n");
	scanf("%d", &info.age);
	printf("请输⼊联系电话：\n");
	scanf("%s", &info.tel);
	printf("请输⼊地址：\n");
	scanf("%s", &info.addr);

	SLPushBack(con, info);
	printf("插⼊成功！\n");

}
//删除通讯录数据
void DelContact(contact** con)
{

	char name[NAME_MAX];
	printf("请输⼊要删除的⽤⼾姓名：\n");
	scanf("%s", name);
	contact* pos = FindByName(*con, name);
	if (pos == NULL) {
		printf("要删除的⽤⼾不存在，删除失败！\n");
		return;
	}
	SLErase(con, pos);
	printf("删除成功！\n");

}
//展示通讯录数据
void ShowContact(contact* con)
{
	printf("%-10s %-4s %-4s %15s %-20s\n", "姓名", "性别", "年龄", "联系电话", "地址");
		contact * cur = con;
		while (cur)
		{
			printf("%-10s %-4s %-4d %15s %-20s\n",
				cur->val.name,
				cur->val.sex,
				cur->val.age,
				cur->val.tel,
				cur->val.addr);
			cur = cur->next;
		}


}
//查找通讯录数据
void FindContact(contact* con)
{
	char name[NAME_MAX];
	printf("请输⼊要查找的⽤⼾姓名：\n");
	scanf("%s", name);
	contact* pos = FindByName(con, name);
	if (pos == NULL) {
		printf("要查找的⽤⼾不存在，查找失败！\n");
		return;
	}
	printf("查找成功！\n");
	printf("%-10s %-4s %-4d %15s %-20s\n",
		pos->val.name,
		pos->val.sex,
		pos->val.age,
		pos->val.tel,
		pos->val.addr);
}
//修改通讯录数据
void ModifyContact(contact** con)
{

	char name[NAME_MAX];
	printf("请输⼊要修改的⽤⼾名称:\n");
	scanf("%s", &name);
	contact* pos = FindByName(*con, name);
	if (pos == NULL) {
		printf("要查找的⽤⼾不存在，修改失败！\n");
		return;
	}
	printf("请输⼊要修改的姓名：\n");
	scanf("%s", pos->val.name);
	printf("请输⼊要修改的性别：\n");
	scanf("%s", pos->val.sex);
	printf("请输⼊要修改的年龄：\n");
	scanf("%d", &pos->val.age);
	printf("请输⼊要修改的联系电话：\n");
	scanf("%s", pos->val.tel);
	printf("请输⼊要修改的地址：\n");
	scanf("%s", pos->val.addr);
	printf("修改成功！\n");

}
////销毁通讯录数据
//void DestroyContact(contact** con)
//{
//
//}